###########################################################
# $Id: xrsh_config.sh 4690 2013-01-08 13:08:18Z xrdk $
# brief	    : 
# author    : xrdk
# version   : V1.0, 2004-03-16
#
# CopyRight (C) 2004-2011 xrdk.
# All rights reserved.
###########################################################

function xrmk_config_get()
{
	xrmk_config_path
	xrmk_get_gcc_config
	xrmk_get_cc_config
	xrmk_get_cc_mode
	xrmk_get_cc_utils
	
	xrmk_get_osdir
	xrmk_get_outdir
	xrmk_setrpath_default
	
	xrmk_get_cc_config_other
	xrmk_config_export
}

function xrmk_config_echo()
{
	echo ---------- Config info --------------------
	echo "--      os dir : $xrmk_os_dir"
	echo "--    spec dir : $xrmk_os_specdir"
	echo 
	echo "--    out home : $xrmk_dir_home"
	echo "--     out bin : $xrmk_dir_bin"
	echo "--     out lib : $xrmk_dir_lib"
	echo "--     out dst : $xrmk_dir_dst"
	echo "--     out obj : $xrmk_dir_obj"
	echo
	echo "--     lib pre : $xrmk_lib_pre"
	echo "--     lib sub : $xrmk_lib_sub"
	echo
	echo "--         log : $xrmk_log"
	echo "--      config : $xrmk_cfg"
	echo "--     defines : $xrmk_defs"
	echo "--         ext : $xrmk_ext"
	echo "--    mak file : $xrmk_mak_file"
	echo "--   mak flags : $xrmk_flags"
	echo "--   debug opt : $xrmk_opts_debug"
	echo "--    c 64 opt : $xrmk_opts_c"
	echo "--   lnk 64 opt: $xrmk_opts_lnk"
	echo
  echo "--     c flags : $xrmk_flags_c"
  echo "--   lnk flags : $xrmk_flags_lnk"
	echo "--    ar flags : $xrmk_flags_ar"
  echo "--    ld flags : $xrmk_flags_ld"
  echo "--   debug flag: $xrmk_flags_debug"
  echo "--   c dso flag: $xrmk_flags_dso_c"
  echo "--   lnk dso f.: $xrmk_flags_dso_lnk"
  echo "--   c lib flag: $xrmk_flags_lib_c"
  echo
  echo "--       rpath : $xrmk_runpath"
  echo "--        PATH : $PATH"
  echo
  echo --------------------------------------------
}

function xrmk_cc_dir_cvt()
{
	local _xrmk_tmp
	
	if [ "$xrmk_xrdk" != "" ]; then
		xrmk_ginc="$xrmk_xrdk $xrmk_xrdk/include $xrmk_ginc"
	fi
	
	if [ "$xrmk_ginc" != "" ] && [ "$xrmk_ginc_i" = "" ]; then
		_xrmk_tmp=`echo ${xrmk_ginc[*]}`
		xrmk_ginc_i=`xrsh_itemcvt_inc "$_xrmk_tmp"`
	fi
	
	if [ "$xrmk_glib" != "" ] && [ "$xrmk_glib_l" = "" ]; then
		_xrmk_tmp=`echo ${xrmk_glib[*]}`
		xrmk_glib_l=`xrsh_itemcvt_lib "$_xrmk_tmp"`
	fi
}

function xrmk_get_dst()
{
	echo "$xrmk_dir_dst/lib$1$xrmk_cfg$xrmk_ext"
}

function xrmk_config_export()
{
	export xrmk_cc
	export xrmk_cpp
	export xrmk_make
	export xrmk_gmake
	export xrmk_lnk
	export xrmk_lnkpp
	export xrmk_ld
	export xrmk_ldpp
	export xrmk_ar
	export xrmk_ranlib
	
	export xrmk_dso_ext
	export xrmk_lib_ext
	export xrmk_exe_ext
	
	export xrmk_lib_sys
	export xrmk_rpath_pre
	export xrmk_rpath_sub
	
	export xrmk_flags_c
	export xrmk_flags_lnk
	export xrmk_flags_ar
	export xrmk_flags_ld
	export xrmk_flags_debug
	
	export xrmk_flags_dso_c
	export xrmk_flags_dso_lnk
	export xrmk_flags_lib_c
	
	export xrmk_cfg_sd
	export xrmk_cfg_64
	export xrmk_cfg
	
	export xrmk_ext
	export xrmk_mak_file
	export xrmk_exe_file
	export xrmk_flags
	
	export xrmk_opts_debug
	export xrmk_opts_c
	export xrmk_opts_lnk
	
	export xrmk_os_specdir
	export xrmk_os_dir
	
	export xrmk_dir_home
	export xrmk_dir_bin
	export xrmk_dir_lib
	export xrmk_dir_obj
	export xrmk_dir_dst
	
	export xrmk_lib_pre
	export xrmk_lib_sub
	export xrmk_runpath
	
	export xrmk_log
	export xrmk_outlog
	
	export xrmk_sinc
	export xrmk_sinc_i
	export xrmk_slib
	export xrmk_slib_l
	
	export xrmk_ar_merge
}

function xrmk_echo_out_dir()
{
	echo ------------
	echo Dir: $1
	$xrsh_ls $1
}

function xrmk_echo_out()
{
  if [ "$xrmk_cmd" = "clean" ] || [ "$xrmk_cmd" = "cdir" ]; then return; fi
  
	echo --------------------------------------------
	echo Cur dir: $xrmk_curdir_pwd
	echo Current: `date`
		
	xrmk_echo_out_dir "$xrmk_dir_dst"
	
	if [ "$xrmk_dir_dst" != "$xrmk_dir_bin" ]; then
		xrmk_echo_out_dir "$xrmk_dir_bin"
	fi
	if [ "$xrmk_dir_dst" != "$xrmk_dir_lib" ]; then
		xrmk_echo_out_dir "$xrmk_dir_lib"
	fi
}
#=====================================================================
function xrmk_get_osdir()
{
	if [ "$xrmk_os_dir" = "" ]; then xrmk_os_dir="$xrmk_os_name"_"$xrmk_os_vdash"_"$xrmk_os_arch" ; fi
	
	if [ "$xrmk_specname" = "auto" ] && [ "$xrmk_os_specname" = "" ]; then xrmk_specname= ; fi
	
	if [ "$xrmk_specname" = "auto" ]; then 
		xrmk_os_specdir="$xrmk_os_specname$xrmk_os_specver"_"$xrmk_os_vdash" 
	elif [ "$xrmk_specname" != "" ]; then
		xrmk_os_specdir="$xrmk_specname"_"$xrmk_os_vdash" 
	else
		xrmk_os_specdir="$xrmk_os_name"_"$xrmk_os_vdash"
	fi
	
	local _xrmk_tmp_arch=$xrmk_os_arch;
	local _xrmk_tmp_ccname=$xrmk_cc_name;
	
	if [ "$_xrmk_tmp_arch" = "x86" ];   then _xrmk_tmp_arch= ; fi
	if [ "$_xrmk_tmp_ccname" = "gcc" ]; then _xrmk_tmp_ccname= ; fi

	if [ "$_xrmk_tmp_arch" != "" ];   then xrmk_os_specdir="$xrmk_os_specdir"_"$_xrmk_tmp_arch" ; fi
	if [ "$_xrmk_tmp_ccname" != "" ]; then xrmk_os_specdir="$xrmk_os_specdir"_"$_xrmk_tmp_ccname" ; fi
}

function xrmk_get_outdir()
{
	if [ "$xrmk_curdir" = "" ];  then xrmk_curdir=. ; fi
	if [ "$xrmk_curdir_pwd" = "" ];  then xrmk_curdir_pwd=`pwd` ; fi
	
	if [ "$xrmk_prodir" = "" ];  then xrmk_prodir=$xrmk_curdir ; fi
	
	if [ "$xrmk_dir_home" = "" ]; then xrmk_dir_home=$xrmk_curdir/$xrmk_os_specdir ; fi
	if [ "$xrmk_dir_bin" = "" ];  then xrmk_dir_bin=$xrmk_dir_home/bin ; fi
	
	if [ "$xrmk_static" = "yes" ]; then
		if [ "$xrmk_dir_lib" = "" ]; then xrmk_dir_lib=$xrmk_dir_home/lib ; fi
		if [ "$xrmk_dir_dst" = "" ]; then xrmk_dir_dst=$xrmk_dir_lib ; fi
	else
		if [ "$xrmk_dir_lib" = "" ]; then xrmk_dir_lib=$xrmk_dir_home/bin ; fi
		if [ "$xrmk_dir_dst" = "" ]; then xrmk_dir_dst=$xrmk_dir_bin ; fi
	fi
	
	if [ "$xrmk_dir_obj" = "" ]; then xrmk_dir_obj=$xrmk_dir_home/obj ; fi
}
#=====================================================================
function xrmk_config_path()
{
	export PATH=$xrmk_cc_home_bin:$PATH
}

function xrmk_get_gcc_config()
{
	if [ "$xrmk_cc_name" != "gcc" ]; then return; fi
	
	if [ "$xrmk_os_arch64" = "yes" ] && [ "$xrmk_flags_32_c" = "" ] && [ "$xrmk_flags_64_c" = "" ]; then
	  xrmk_flags_32_c="-m32"
	  xrmk_flags_64_c="-m64"
	fi
	
	if [ "$xrmk_os_arch64" = "no" ]; then
		xrmk_flags_32_c=
		xrmk_flags_64_c=
		xrmk_flags_32_lnk=
	fi
		
	if [ "$xrmk_cc" = "" ];  then xrmk_cc=`xrsh_getexepath "gcc" "$xrmk_cc_home_bin/gcc"` ; fi
	if [ "$xrmk_cpp" = "" ]; then xrmk_cpp=`xrsh_getexepath "g++" "$xrmk_cc_home_bin/g++"` ; fi

	if test "$xrmk_get_cc_ver" = ""; then
		xrmk_get_cc_ver="$xrmk_cc --version"
  	xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/.*\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
		xrmk_cc_vmaj=`xrsh_getdotnum $xrmk_cc_ver`
	fi

	if test "$xrmk_flags_hide" = "no"; then
	  xrmk_flags_hide=
	elif [ $(($xrmk_cc_vmaj+0)) -ge 4 ]; then
	  xrmk_flags_hide="-fvisibility=hidden"
	fi
	
	if test "$xrmk_flags_asneed" = "no"; then
	  xrmk_flags_asneed=
	elif [ $(($xrmk_cc_vmaj+0)) -ge 4 ]; then
	  xrmk_flags_asneed="-Wl,-x -Wl,--as-needed"
	fi
}
#=====================================================================
function xrmk_get_cc_config()
{
	xrmk_flags_c="$xrmk_flags_c $xrmk_os_defs"
	xrmk_flags_lnk="$xrmk_flags_lnk"
	
	xrmk_flags_dso_c="$xrmk_flags_dso_c $xrmk_flags_hide -DXRCC_DSO -D_DSO"
	xrmk_flags_dso_lnk="$xrmk_flags_dso_lnk $xrmk_flags_asneed"
	
	xrmk_flags_lib_c="$xrmk_flags_lib_c -DXRCC_STATIC -D_LIB"
	
  if test "$xrmk_debug" = "yes"; then
    xrmk_opts_debug="$xrmk_flags_debug -DXRCC_DEBUG -D_DEBUG"
  else
    xrmk_opts_debug=
  fi
  
  if test "$xrmk_static" = "yes"; then
    xrmk_flags=$xrmk_flags_lib_c
  else
    xrmk_flags=$xrmk_flags_dso_c
  fi
  
  if test "$xrmk_64" = "yes"; then
    xrmk_opts_c=$xrmk_flags_64_c
    xrmk_opts_lnk=$xrmk_flags_64_lnk
    $xrmk_set_c_env_64
  else
    xrmk_opts_c=$xrmk_flags_32_c
    xrmk_opts_lnk=$xrmk_flags_32_lnk
    $xrmk_set_c_env_32
  fi
  
  if test "$xrmk_static" = "yes"; then
    xrmk_ext=$xrmk_lib_ext
    xrmk_mak_file="xrmake_lib"
    xrmk_exe_file="xrmake_exe_lib"
  else
    xrmk_ext=$xrmk_dso_ext
    xrmk_mak_file="xrmake_dso"
    xrmk_exe_file="xrmake_exe_dso"
  fi
}

function xrmk_get_cc_config_other()
{
	if [ "$xrmk_os_name" = "lnx" ] && [ "$xrmk_os_arch64" != "yes" ]; then
	  xrmk_lib_pre="-l"
	  xrmk_lib_sub=
	fi
	
	if [ "$xrmk_lib_pre" = "usedir" ]; then xrmk_lib_pre=$xrmk_dir_lib/lib ; fi
	if [ "$xrmk_lib_sub" = "useext" ]; then xrmk_lib_sub=$xrmk_ext ; fi

	if [ "$xrmk_has_rpath" = "no" ]; then
	  xrmk_rpath_pre=
	  xrmk_rpath_sub=
	else
		xrmk_setrpath "$xrmk_dir_dst"
	fi
	
	xrmk_log=$xrmk_dir_home/compile".log"
	xrmk_outlog=">> $xrmk_log 2>&1"
}

function xrmk_get_cc_mode()
{
  if test "$xrmk_64" = "yes"; then
    xrmk_cfg_64=_64
  else
    xrmk_cfg_64=
  fi

  if test "$xrmk_static" = "yes" && test "$xrmk_debug" = "yes"; then
    xrmk_cfg_sd="sd"
  elif test "$xrmk_static" = "yes"; then
    xrmk_cfg_sd="s"
  elif test "$xrmk_debug" = "yes"; then
    xrmk_cfg_sd="d"
  else
  	xrmk_cfg_sd=
  fi
     
  if test "$xrmk_cfg_sd" = ""; then
    xrmk_cfg=$xrmk_cfg_64
  else
    xrmk_cfg=$xrmk_cfg_64"_"$xrmk_cfg_sd
  fi
}

function xrmk_get_cc_utils()
{
	if [ "$xrmk_cpp" = "" ];   then xrmk_cpp=$xrmk_cc ; fi
	if [ "$xrmk_lnk" = "" ];   then xrmk_lnk=$xrmk_cc ; fi
	if [ "$xrmk_lnkpp" = "" ]; then xrmk_lnkpp=$xrmk_cpp ; fi
	if [ "$xrmk_ld" = "" ];    then xrmk_ld=$xrmk_cc ; fi
	if [ "$xrmk_ldpp" = "" ];  then xrmk_ldpp=$xrmk_cpp ; fi
	
	if [ "$xrmk_make" = "" ];  then xrmk_make=`xrsh_getexepath "make"` ; fi
	if [ "$xrmk_gmake" = "" ]; then xrmk_gmake=`xrsh_getexepath "gmake"` ; fi
	
	if test "$xrmk_make" = ""; then
	  if test "$xrmk_gmake" = ""; then
	    xrmk_make=$xrmk_cc_home_bin/make
	  else
	    xrmk_make=$xrmk_gmake
	  fi
	fi
	
	if test "$xrmk_gmake" = ""; then
	  if test "$xrmk_make" = ""; then
	    xrmk_gmake=$xrmk_cc_home_bin/gmake
	  else
	    xrmk_gmake=$xrmk_make
	  fi
	fi
	
	if [ "$xrmk_ar" = "" ];     then xrmk_ar=`xrsh_getexepath "ar"` ; fi
	if [ "$xrmk_ranlib" = "" ]; then xrmk_ranlib=`xrsh_getexepath "ranlib"` ; fi
}
#=====================================================================
function xrmk_setrpath()
{
  xrmk_runpath="$xrmk_runpath $xrmk_rpath_pre$1$xrmk_rpath_sub"
}

function xrmk_setrpath_default()
{
	if test "$xrmk_has_rpath" = "no"; then return ; fi
	
  xrmk_setrpath "."
  xrmk_setrpath "lib"
  xrmk_setrpath "bin"
  xrmk_setrpath "../bin"
  xrmk_setrpath "../lib"
  xrmk_setrpath "/usr/local/lib"
  xrmk_setrpath "/usr/lib"
}
#=====================================================================
#=====================================================================
function xrmk_foreach_ex()
{
  local _xrmk_tmp
  local _xrmk_cnt
  local _xrmk_array=`echo "$1"`
  
  #if rebuild, first clean
  if test "$xrmk_cmd" = "rebuild"; then
    _xrmk_cnt=0
    echo
    echo ---- str clean ----------------
    
    xrmk_cmd=clean
    
    for _xrmk_tmp in ${_xrmk_array[*]}; do
      $2 $_xrmk_cnt $_xrmk_tmp $xrmk_cmd
      _xrmk_cnt=$(( $_xrmk_cnt + 1 ))
    done
    
    xrmk_cmd=
    
    echo ---- end clean ----------------
    echo
  fi
  
  _xrmk_cnt=0
  for _xrmk_tmp in ${_xrmk_array[*]}; do
    $2 $_xrmk_cnt $_xrmk_tmp $xrmk_cmd
    _xrmk_cnt=$(( $_xrmk_cnt + 1 ))
  done
}

function xrmk_get_before_mak()
{
	xrmk_cc_dir_cvt
	
	export xrmk_defs
	
	export xrmk_curdir
	export xrmk_curdir_pwd
	export xrmk_shdir
	
	export xrmk_ginc
	export xrmk_ginc_i
	export xrmk_glib
	export xrmk_glib_l
	export xrmk_gdefs
	
	export xrmk_xrdk
}

function xrmk_get_usr_cfg_mode()
{
	local _xrmk_tmp=$xrmk_cfg_usr$xrmk_cfg_sd
	
  if test "$_xrmk_tmp" = ""; then
    xrmk_cfg=$xrmk_cfg_64
  else
    xrmk_cfg=$xrmk_cfg_64"_"$_xrmk_tmp
  fi
}

function xrmk_get_usr_mak_file()
{
	if test "$xrmk_pmak_cmn" = ""; then xrmk_pmak_cmn=$xrmk_prodir/project_cmn.mak ; fi
	if test "$xrmk_pmak_rules" = ""; then xrmk_pmak_rules=$xrmk_prodir/project_rules.mak ; fi
	
	if $xrsh_xfile $xrmk_pmak_cmn; then
		xrmk_pmak_cmn=$xrmk_pmak_cmn
	else
		xrmk_pmak_cmn=$xrmk_shdir/xrmake_empty.mak
	fi
	
	if $xrsh_xfile $xrmk_pmak_rules; then
		xrmk_pmak_rules=$xrmk_pmak_rules
	else
		xrmk_pmak_rules=$xrmk_shdir/xrmake_empty.mak
	fi
	
	export xrmk_pmak_cmn
	export xrmk_pmak_rules
}

xrmk_before_mak_inited="no"

function xrmk_foreach()
{
	if [ "$xrmk_before_mak_inited" != "yes" ]; then 
		xrmk_get_before_mak
		xrmk_get_usr_mak_file
		xrmk_before_mak_inited="yes"
	fi
	
	xrmk_get_usr_cfg_mode
	
	xrmk_opts_cmd

  local _xrmk_tmp
  local _xrmk_cnt
  local _xrmk_array=`echo "$1"`
  local _xrmk_cmd=$xrmk_cmd
  
  if [ "$xrmk_cmd" != "cdir" ] && [ "$xrmk_cmd" != "check" ]; then
  	xrmk_in_build="yes"
  else
  	xrmk_in_build="no"
  fi
  
  #if rebuild, first clean
  if test "$xrmk_cmd" = "rebuild"; then
    _xrmk_cnt=0
    echo
    echo ---- begin clean -----------
    
    xrmk_cmd=clean
    
    for _xrmk_tmp in ${_xrmk_array[*]}; do
      $xrmk_gmake -f $_xrmk_tmp.mak $xrmk_cmd
      _xrmk_cnt=$(( $_xrmk_cnt + 1 ))
    done
    
    xrmk_cmd=
    
    echo ---- end clean -------------
  fi
  
  _xrmk_cnt=0
  for _xrmk_tmp in ${_xrmk_array[*]}; do
  	if [ "$xrmk_cmd" != "cdir" ]; then
  		echo
  		echo "---- begin $_xrmk_tmp ------------>"
  	
		  if [ "$2" != "" ]; then
		  	$2 $_xrmk_cnt $_xrmk_tmp $xrmk_cmd
		  fi
	  fi
    	
    $xrmk_gmake -f $_xrmk_tmp.mak $xrmk_cmd
    
	  if [ "$3" != "" ] && [ "$xrmk_cmd" != "cdir" ]; then
	  	$3 $_xrmk_cnt $_xrmk_tmp $xrmk_cmd
	  fi

    _xrmk_cnt=$(( $_xrmk_cnt + 1 ))
    
    if [ "$xrmk_cmd" != "cdir" ]; then
    	echo "<----- end $_xrmk_tmp -------------"
    fi
    
  done
  
  xrmk_echo_out
  
  xrmk_cmd=$_xrmk_cmd
}

#use ar utils merge tow static libs
#args: 1 dst lib; 2 lib 1; 3 lib 2; 4 tmp dir(option)
function xrmk_ar_merge()
{
  echo merge lib: 
  echo -- .dst : $1 
  echo -- lib 1: $2
  echo -- lib 2: $3
  
  local _xrmk_curdir=`pwd`
  local _xrmk_tmp=$4
  if [ "$_xrmk_tmp" = "" ]; then
  	_xrmk_tmp=$_xrmk_curdir/tmp
  fi
  
  echo --  tmp : $_xrmk_tmp
  echo --  cur : $_xrmk_curdir
  
  $xrsh_xdir $_xrmk_tmp/ || $xrsh_mkdir $_xrmk_tmp/
  
  $xrsh_cp_f $2 $_xrmk_tmp/l1
  $xrsh_cp_f $3 $_xrmk_tmp/l2
  
  $xrsh_cd $_xrmk_tmp
  $xrsh_rm_f *.o
  $xrmk_ar x l1
  $xrmk_ar x l2
  
  $xrsh_cd $_xrmk_curdir
  
  $xrsh_rm_f $1
  $xrmk_ar cru $1 $_xrmk_tmp/*.o
  $xrmk_ranlib $1
  
  $xrsh_rm_f $_xrmk_tmp/*
}


